export const parse = (str, onError) => {
  if (str && typeof str === 'string') {
    try {
      return JSON.parse(str)
    } catch (e) {
      onError && onError(e)
      return {}
    }
  } else {
    return str || {}
  }
}

export const fomatter = searchParams => {
  const params = {}
  searchParams.forEach((value, key) => {
    params[key] = value
  })
  return params
}

export const getQuery = () => {
  const search = new URLSearchParams(window.location.search.replace(/^\?/, ''))
  const hash = new URLSearchParams(window.location.hash.replace(/^#\/\??/, ''))
  // 测试环境skillId=3404
  // GaIroEVrWQFpevbZQvas000000
  // GPxGn0ckD60pPtqq3yoi000000
  const query = Object.assign(
    {
      devId: 'GaIroEVrWQFpevbZQvas000000',
      pk: 'a1z9CBa8Rmw',
      skillId: '3404'
    },
    fomatter(search),
    fomatter(hash)
  )
  return query
}
